﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernateSample.Domain.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace NHibernateSample.Data
{
    public class Transction
    {
        protected ISession Session { get; set; }
        public Transction()
        {
            Session = new NHibernateHelper().GetSession();
        }

        public void UpdateCustomer(int customerID,string newFirstName)
        {
            ITransaction tran = Session.BeginTransaction();
            try
            {
                Customer customer = Session.CreateCriteria<Customer>().Add(Expression.Eq("Id", customerID)).UniqueResult<Customer>();
                customer.FirstName = newFirstName;
                Session.SaveOrUpdate(customer);
                tran.Commit();
            }
            catch(Exception ex)
            {
                tran.Rollback();
            }
        }


        public Customer Get(int customerID)
        {
            return Session.Get<Customer>(customerID);
        }
    }
}
